package backend.dal.hibernate;

import java.util.Collections;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import backend.dal.DALException;
import backend.dal.UserDAO;
import backend.dal.jdbc.JdbcUserDAO;
import backend.model.User;

public class HibernateUserDAO extends HibernateDAO<User> implements UserDAO {

	private static final Logger LOG = LoggerFactory
			.getLogger(JdbcUserDAO.class);

	@Override
	public User getUserById(int id) throws DALException {
		User user = new User();
		Session session = null;
		try {
			session = SessionManager.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			final Criteria c = session.createCriteria(User.class).add (
					Restrictions.eq("userId", id ));
			user = (User) c.uniqueResult();
//			user = (User) session.createQuery(
//					"from User where userId='" + id + "'").uniqueResult();
			session.getTransaction().commit();
		} catch (final HibernateException ex) {
			if (session != null && session.getTransaction() != null) {
				session.getTransaction().rollback();
			}
			LOG.error("User selection failed", ex);
			throw new DALException("User selection failed");
		}
		return user;
	}

	@Override
	public void insertUser(User user) throws DALException {
		Session session = null;
		try {
			session = SessionManager.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.persist(user);
			session.getTransaction().commit();
			LOG.info("User succesfully inserted");
		} catch (final HibernateException ex) {
			if (session != null && session.getTransaction() != null) {
				session.getTransaction().rollback();
			}
			LOG.error("User insertion failed", ex);
			throw new DALException("User insertion failed");
		}
	}

	@Override
	public void updateUser(User user) throws DALException {
		Session session = null;
		try {
			session = SessionManager.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.update(user);
			session.getTransaction().commit();
			LOG.info("User succesfully updated");
		} catch (final HibernateException ex) {
			if (session != null && session.getTransaction() != null) {
				session.getTransaction().rollback();
			}
			LOG.error("User update failed", ex);
			throw new DALException("User update failed");
		}
	}

	@Override
	public void deleteUser(Integer id) throws DALException {
		Session session = null;
		User user = new User();
		user.setUserId(id);
		try {
			session = SessionManager.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.delete(user);
			session.getTransaction().commit();
			LOG.info("User succesfully deleted");
		} catch (final HibernateException ex) {
			if (session != null && session.getTransaction() != null) {
				session.getTransaction().rollback();
			}
			LOG.error("User deletion failed", ex);
			throw new DALException("User deletion failed");
		}
	}

	@Override
	public String getUserPassword(Integer id) throws DALException {
		String password = "";
		Session session= null;
		try {
		session = SessionManager.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		password =  (String) session.createQuery(
				"SELECT password from User where userId='" + id + "'").uniqueResult();
		session.getTransaction().commit ();
		LOG.info("User password selected");
		} catch (final HibernateException ex) {
		if (session != null && session.getTransaction() != null) {
		session.getTransaction().rollback ();
		}
		LOG.error("User password selection failed", ex);
		throw new DALException("User password selection failed");
		}
		return password;
	}

	@Override
	public List<User> viewAllUsers() throws DALException {
		List<User> userList = Collections.emptyList();
		Session session = null;
		try {
			session = SessionManager.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			userList = session.createQuery("from User").list();
			session.getTransaction().commit();
			LOG.info("Users succesfully displayed");
		} catch (final HibernateException ex) {
			if (session != null && session.getTransaction() != null) {
				session.getTransaction().rollback();
			}
			LOG.error("User display failed", ex);
			throw new DALException("Book display failed");
		}
		return userList;
	}

}
